﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace System.Web
{
    internal class HttpValueCollection : NameValueCollection
    {
        internal HttpValueCollection(string str, bool urlencoded)
            : base(StringComparer.OrdinalIgnoreCase)
        {
            if (!string.IsNullOrEmpty(str))
            {
                this.FillFromString(str, urlencoded);
            }
        }
        internal void FillFromString(string s, bool urlencoded)
        {
            int num = (s != null) ? s.Length : 0;
            for (int i = 0; i < num; i++)
            {
                int num2 = i;
                int num3 = -1;
                while (i < num)
                {
                    char c = s[i];
                    if (c == '=')
                    {
                        if (num3 < 0)
                        {
                            num3 = i;
                        }
                    }
                    else
                    {
                        if (c == '&')
                        {
                            break;
                        }
                    }
                    i++;
                }
                string text = null;
                string text2;
                if (num3 >= 0)
                {
                    text = s.Substring(num2, num3 - num2);
                    text2 = s.Substring(num3 + 1, i - num3 - 1);
                }
                else
                {
                    text2 = s.Substring(num2, i - num2);
                }
                if (urlencoded)
                {
                    base.Add(Uri.UnescapeDataString(text), Uri.UnescapeDataString(text2));
                }
                else
                {
                    base.Add(text, text2);
                }
                if (i == num - 1 && s[i] == '&')
                {
                    base.Add(null, string.Empty);
                }
            }
        }

        internal void Reset()
        {
            base.Clear();
        }
        public override string ToString()
        {
            return this.ToString(true);
        }
        internal virtual string ToString(bool urlencoded)
        {
            return this.ToString(urlencoded, null);
        }
        internal virtual string ToString(bool urlencoded, IDictionary excludeKeys)
        {
            int count = this.Count;
            if (count == 0)
            {
                return string.Empty;
            }
            StringBuilder stringBuilder = new StringBuilder();
            bool flag = excludeKeys != null && excludeKeys["__VIEWSTATE"] != null;
            for (int i = 0; i < count; i++)
            {
                string text = this.GetKey(i);
                if ((!flag || text == null || !text.StartsWith("__VIEWSTATE", StringComparison.Ordinal)) && (excludeKeys == null || text == null || excludeKeys[text] == null))
                {
                    if (urlencoded)
                    {
                        text = Uri.EscapeUriString(text);
                    }
                    string value = (text != null) ? (text + "=") : string.Empty;
                    ArrayList arrayList = (ArrayList)base.BaseGet(i);
                    int num = (arrayList != null) ? arrayList.Count : 0;
                    if (stringBuilder.Length > 0)
                    {
                        stringBuilder.Append('&');
                    }
                    if (num == 1)
                    {
                        stringBuilder.Append(value);
                        string text2 = (string)arrayList[0];
                        if (urlencoded)
                        {
                            text2 = Uri.EscapeUriString(text2);
                        }
                        stringBuilder.Append(text2);
                    }
                    else
                    {
                        if (num == 0)
                        {
                            stringBuilder.Append(value);
                        }
                        else
                        {
                            for (int j = 0; j < num; j++)
                            {
                                if (j > 0)
                                {
                                    stringBuilder.Append('&');
                                }
                                stringBuilder.Append(value);
                                string text2 = (string)arrayList[j];
                                if (urlencoded)
                                {
                                    text2 = Uri.EscapeUriString(text2);
                                }
                                stringBuilder.Append(text2);
                            }
                        }
                    }
                }
            }
            return stringBuilder.ToString();
        }
    }
}
